package cn.iocoder.yudao.module.museum.service.collectionimageacquisition;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.museum.controller.admin.collectionimageacquisition.vo.CollectionImageAcquisitionPageReqVO;
import cn.iocoder.yudao.module.museum.controller.admin.collectionimageacquisition.vo.CollectionImageAcquisitionSaveReqVO;
import cn.iocoder.yudao.module.museum.controller.admin.collectionmanagement.vo.CollectionImageImportExcelVO;
import cn.iocoder.yudao.module.museum.controller.admin.collectionmanagement.vo.CollectionImportRespVO;
import cn.iocoder.yudao.module.museum.dal.dataobject.collectionimageacquisition.CollectionImageAcquisitionDO;
import cn.iocoder.yudao.module.museum.dal.dataobject.collectionmanagement.CollectionManagementDO;
import cn.iocoder.yudao.module.museum.dal.mysql.collectionimageacquisition.CollectionImageAcquisitionMapper;
import cn.iocoder.yudao.module.museum.service.collectionmanagement.CollectionManagementService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.museum.enums.ErrorCodeConstants.COLLECTION_IMAGE_ACQUISITION_NOT_EXISTS;

/**
 * 藏品二维采集信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CollectionImageAcquisitionServiceImpl implements CollectionImageAcquisitionService {

    @Resource
    private CollectionImageAcquisitionMapper collectionImageAcquisitionMapper;


    @Override
    public Integer createCollectionImageAcquisition(CollectionImageAcquisitionSaveReqVO createReqVO) {
        // 插入
        CollectionImageAcquisitionDO collectionImageAcquisition = BeanUtils.toBean(createReqVO, CollectionImageAcquisitionDO.class);
        collectionImageAcquisitionMapper.insert(collectionImageAcquisition);
        // 返回
        return collectionImageAcquisition.getId();
    }

    @Override
    public void updateCollectionImageAcquisition(CollectionImageAcquisitionSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectionImageAcquisitionExists(updateReqVO.getId());
        // 更新
        CollectionImageAcquisitionDO updateObj = BeanUtils.toBean(updateReqVO, CollectionImageAcquisitionDO.class);
        collectionImageAcquisitionMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollectionImageAcquisition(Long id) {
        // 校验存在
        validateCollectionImageAcquisitionExists(id);
        // 删除
        collectionImageAcquisitionMapper.deleteById(id);
    }

    private void validateCollectionImageAcquisitionExists(Long id) {
        if (collectionImageAcquisitionMapper.selectById(id) == null) {
            throw exception(COLLECTION_IMAGE_ACQUISITION_NOT_EXISTS);
        }
    }

    @Override
    public CollectionImageAcquisitionDO getCollectionImageAcquisition(Long id) {
        return collectionImageAcquisitionMapper.selectById(id);
    }

    @Override
    public PageResult<CollectionImageAcquisitionDO> getCollectionImageAcquisitionPage(CollectionImageAcquisitionPageReqVO pageReqVO) {
        return collectionImageAcquisitionMapper.selectPage(pageReqVO);
    }

    @Override
    public CollectionImportRespVO saveImportDataList(CollectionManagementService managementService,List<CollectionImageImportExcelVO> iList, Boolean updateSupport, Integer importType) {
        CollectionImportRespVO respVO = CollectionImportRespVO.builder().createNames(new ArrayList<>())
                .updateNames(new ArrayList<>()).failureNames(new LinkedHashMap<>()).build();
        // 校验数据
        CollectionImageAcquisitionDO collection3dAcquisitionDO;
        for (CollectionImageImportExcelVO d : iList) {
            collection3dAcquisitionDO = new CollectionImageAcquisitionDO();

            CollectionManagementDO collectionManagement = managementService.selectByGeneralAccessionNumber(d.getGeneralAccessionNumber());
            if(Objects.isNull(collectionManagement)){
                respVO.getFailureNames().put(d.getImageFilename(), "藏品总登记号不存在");
                continue;
            }
            CollectionImageAcquisitionDO exist = collectionImageAcquisitionMapper.selectByImageFilename(collectionManagement.getId(),d.getImageFilename());
            if (exist != null) {
                if (updateSupport) {
                    BeanUtils.copyProperties(d, collection3dAcquisitionDO);
                    collection3dAcquisitionDO.setCollectionId(collectionManagement.getId());
                    collection3dAcquisitionDO.setId(exist.getId());
                    collectionImageAcquisitionMapper.updateById(collection3dAcquisitionDO);
                    respVO.getUpdateNames().add(d.getImageFilename());
                } else {
                    respVO.getFailureNames().put(d.getImageFilename(), "失败，影像文件名已存在");
                    //throw exception(GlobalErrorCodeConstants.COLLECTION3D_ACQUISITION_EXISTS);
                }
                continue;
            }

            BeanUtils.copyProperties(d, collection3dAcquisitionDO);
            collection3dAcquisitionDO.setCollectionId(collectionManagement.getId());
            collectionImageAcquisitionMapper.insert(collection3dAcquisitionDO);
            respVO.getCreateNames().add(d.getImageFilename());

        }
        return respVO;
    }
}